//
//  HNSH_RefundDetailViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/1.
//

#import "HNSH_RefundDetailViewController.h"

#import "HNSH_OrderDetailCell.h"

#import "HNSH_RefundDetailHeaderView.h"
#import "HNSH_RefundDetailHeader.h"

@interface HNSH_RefundDetailViewController ()

/* model */
@property (nonatomic, strong) HNSH_RefundDetailModel *model;

/* headerView */
@property (nonatomic, strong) HNSH_RefundDetailHeaderView *headerView;

/* dataArray */
@property (nonatomic, strong) NSArray *dataArray;

@end

static NSString *HNSH_OrderDetailCellID = @"HNSH_OrderDetailCellID";
static NSString *HNSH_RefundDetailHeaderID = @"HNSH_RefundDetailHeaderID";

@implementation HNSH_RefundDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showNavTitle:@"退款详情" showLeftItem:YES];
    self.navView.backgroundColor = [UIColor clearColor];
    self.navView.titleLabel.textColor = [UIColor appWhiteLabelColor];
    [self.navView.leftButton setImage:[UIImage imageNamed:@"base_nav_back_white"] forState:UIControlStateNormal];
    
    [self loadNewData];
    [self configTableView];
}

- (void)configTableView {
    self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, kBottomSafeHeight+55, 0));
    }];
    [self.tableView registerClass:[HNSH_OrderDetailCell class] forCellReuseIdentifier:HNSH_OrderDetailCellID];
    [self.tableView registerClass:[HNSH_RefundDetailHeader class] forHeaderFooterViewReuseIdentifier:HNSH_RefundDetailHeaderID];
    self.tableView.rowHeight = 40;
}

//MARK: 订单详情
- (void)loadNewData{
    [HNSH_ShopNetServer requestGoodsOrderAfterSalesInfoWithOrderId:self.orderId success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            NSDictionary * data = responseObject.resultset;
            HNSH_RefundDetailModel * model = [HNSH_RefundDetailModel yy_modelWithDictionary:data];
            self.model = model;
            self.headerView = nil;
            self.tableView.tableHeaderView = self.headerView;
            self.headerView.model = model;
            [self setDefaultData];
            [self.tableView reloadData];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)setDefaultData{
    self.dataArray = self.model.expense_items;
}

//MARK: Getter Methods
- (HNSH_RefundDetailHeaderView *)headerView{
    if (!_headerView) {
        CGFloat h = self.model.middle_show ? (50 + 8) : 0;
        _headerView = [[HNSH_RefundDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavBarAndStatusBarHeight + 112 + 8 + 160 + 8 + h)];
    }
    return _headerView;
}

//MARK: UITableViewDelegate Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.model.account_items.count > 0) {
        return 2;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.model.account_items.count > 0 && section == 1) {
        return self.model.account_items.count;
    }
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (self.model.account_items.count > 0 && section == 1) {
        return 58;
    }
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (self.model.account_items.count > 0 && section == 1) {
        HNSH_RefundDetailHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HNSH_RefundDetailHeaderID];
        return header;
    }
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HNSH_OrderDetailCell * cell = [tableView dequeueReusableCellWithIdentifier:HNSH_OrderDetailCellID];
    if (self.model.account_items.count > 0 && indexPath.section == 1) {
        cell.model = self.model.account_items[indexPath.row];
    } else {
        cell.model = self.dataArray[indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
